package logic

import (
	"context"
	"doctor-medicine-system/model/mysql"
	"errors"

	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MediAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMediAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MediAddLogic {
	return &MediAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MediAddLogic) MediAdd(in *doctorrpc.MediAddRequest) (*doctorrpc.MediAddResponse, error) {
	// todo: add your logic here and delete this line

	var medi mysql.Medi
	mysql.DB.Debug().Where("name = ?", in.Name).Find(&medi)
	if medi.ID != 0 {
		return nil, errors.New("当前药品已存在，请更换")
	}

	m := mysql.Medi{
		Name:      in.Name,
		MediImage: in.MediImage,
		Price:     in.Price,
		Start:     in.Start,
	}
	var doctor mysql.Doctor
	if doctor.ID != m.ID {
		return nil, errors.New("暂无此权限")
	}

	mysql.DB.Debug().Create(&m)

	return &doctorrpc.MediAddResponse{
		MediId: int64(m.ID),
	}, nil
}
